package com.kazimsoylu.gui;

import javax.jws.Oneway;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

import com.kazimsoylu.Application;

import net.sf.jml.MsnContact;
import net.sf.jml.message.MsnInstantMessage;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JLabel;

public class ConversationWindow extends JFrame implements KeyListener
{

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JEditorPane editorPane;
	private MsnContact contact;
	private JTextPane textPane;
	private JLabel statusLabel;
	private Timer statusTimer;

	public ConversationWindow() 
	{
		initializeComponents();
	}
	
	public ConversationWindow( MsnContact contact ) 
	{
		initializeComponents();
		this.contact = contact;
		setTitle( contact.getDisplayName() );
	}

	private void initializeComponents()
	{
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 542, 394);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		editorPane = new JEditorPane();
		editorPane.setEditable(false);
		editorPane.setBounds(10, 11, 506, 239);		
		contentPane.add(editorPane);
		
		textPane = new JTextPane();
		textPane.setBounds(10, 261, 506, 61);
		textPane.addKeyListener( this );
		contentPane.add(textPane);
		
		JButton btnGnder = new JButton("G\u00F6nder");
		btnGnder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				sendMessage();								
			}
		});
		btnGnder.setBounds(428, 334, 89, 23);
		contentPane.add(btnGnder);
		
		statusLabel = new JLabel();
		statusLabel.setBounds(12, 338, 249, 15);
		contentPane.add(statusLabel);
		
		this.addWindowListener( new WindowAdapter() 
		{
			public void windowClosing(WindowEvent e)
			{
				Application.removeConversation( contact );
				Application.removeSwitchBoard(contact);
			}
		});
		
		statusTimer = new Timer( 5000 , new ActionListener() 
		{	
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				clearStatusText();
				statusTimer.stop();
			}
		});
	
	}
	
	private void sendMessage()
	{
		MsnInstantMessage msg = new MsnInstantMessage();
		msg.setContent( textPane.getText() );
		addMessage( textPane.getText() );
		
		Application.getSwitchBoard( contact ).sendMessage( msg );
		textPane.setText( "" );		
		
	}
	
	public void addMessage( MsnInstantMessage message )
	{
		if( editorPane.getText().length()==0 )
			editorPane.setText( message.getContent() );
		else
			editorPane.setText( editorPane.getText()+"\n"+message.getContent() );
	}
	
	public void addMessage( String message )
	{
		if( editorPane.getText().length()==0 )
			editorPane.setText( message );
		else
			editorPane.setText( editorPane.getText()+"\n"+message );
	}
	
	public void setStatusText(String text)
	{
		statusLabel.setText( text );
		
		if(!statusTimer.isRunning())
			statusTimer.start();
	}
	
	public void clearStatusText()
	{
		statusLabel.setText("");
	}

	@Override
	public void keyPressed(KeyEvent e) 
	{
		if( e.getKeyCode() == KeyEvent.VK_ENTER )
			sendMessage();
	}

	@Override
	public void keyReleased(KeyEvent e) 
	{
		
	}

	@Override
	public void keyTyped(KeyEvent e) 
	{
		
	}
}
